package com.hxc.pay.wx.core;

import com.alibaba.fastjson.JSON;
import com.hxc.pay.JsapiPayTemplate;
import com.hxc.pay.wx.config.WxPayProperties;
import com.hxc.pay.wx.dto.Amount;
import com.hxc.pay.wx.dto.JsapiPayDto;
import com.hxc.pay.wx.dto.Payer;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class WxJsapiPayTemplate implements JsapiPayTemplate {


    private WxPayProperties wxPayProperties;
    private CloseableHttpClient httpClient;

    public WxJsapiPayTemplate(WxPayProperties wxPayProperties, CloseableHttpClient httpClient) {
        this.wxPayProperties = wxPayProperties;
        this.httpClient = httpClient;
    }

    @Override
    public String createOrder(String openId, Integer total, String description, String outTradeNo) {

        //请求URL
        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");

        // 请求body参数
        Payer payer = Payer.builder()
                .openid(openId)
                .build();
        Amount amount = Amount.builder()
                .total(1)
                .currency("CNY")
                .build();
        JsapiPayDto jsapiPayDto = JsapiPayDto.builder()
                .appid(wxPayProperties.getAppid())
                .mchid(wxPayProperties.getMchId())
                .description(description)
                .out_trade_no(outTradeNo) //自定义即可
                .notify_url(wxPayProperties.getNotifyUrl()) //暂时不管理，给上即可
                .amount(amount)
                .payer(payer)
                .build();

        String reqdata = JSON.toJSONString(jsapiPayDto);
        StringEntity entity = new StringEntity(reqdata,"utf-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        String resStr = null;

        try {
            //完成签名并执行请求
            CloseableHttpResponse response = httpClient.execute(httpPost);
            resStr = "";
            try {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
                    resStr = EntityUtils.toString(response.getEntity());
                } else if (statusCode == 204) {
                    System.out.println("success");
                } else {
                    System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
                   
                }
            } finally {
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resStr;
    }
}
